package dabs.engine.plant;

import static org.junit.Assert.*;

import org.junit.Before;
import org.junit.Test;

import dabs.engine.DummyInterface;
import dabs.engine.control.ComponentCommand;
import dabs.engine.exceptions.ComponentNotFoundException;
import dabs.engine.messaging.NotificationHandler;
import dabs.ginterface.CommandParser;

public class HydraulicTest  {
	
	DummyInterface dinterface;
	ComponentPart output, turbine,turbine2, valve1, valve2, pump, reactor, condenser;
	ComponentCommand command;
	CommandParser parser;
	
	
	@Before
	public void setUp() {
		
		dinterface = new DummyInterface();
		 turbine = new Turbine(dinterface, "turbine", 100, 10);
		 turbine2 = new Turbine(dinterface, "turbine2", 200, 10);
		 valve1 = new Valve(dinterface, "valve", true, 50, 5);
		 valve2 = new Valve(dinterface, "valve", true, 50, 10);
		 condenser = new Condenser(dinterface, "condenser", 20, 100, 20);
		 pump = new Pump(dinterface, "pump", 20, 20);
		 reactor = new Reactor(dinterface, "reactor", 50, 100, 20);
		
		turbine.setOutputs(valve1);
		turbine2.setOutputs(valve2);
		valve1.setOutputs(condenser);		
		condenser.setOutputs(pump);
		pump.setOutputs(valve2);
		valve2.setOutputs(reactor);
//		reactor.setOutputs(turbine);  
		
		parser = new CommandParser();
	}

	@Test
	public void testHealtVsCapacity()    {
				
		turbine.getNewHydroState().receive(100, false);		
		turbine2.getNewHydroState().receive(100, false);
		
		thisTick();
		
		//both of them get the same amounts of steam,
		//health is damaged less in the one which has bigger capacity
		assertTrue(turbine.getHealth()<turbine2.getHealth()); 
		//reset health and test for water
		turbine.setHealth(100);
		turbine2.setHealth(100);
		turbine.getNewHydroState().receive(100, true);		
		turbine2.getNewHydroState().receive(100, true);
			thisTick();
		assertTrue(turbine.getHealth()<turbine2.getHealth());  	
	}
	
	@Test
	public void testOutRate(){
		valve1.getNewHydroState().receive(30, false);
		valve2.getNewHydroState().receive(30, false);
		
		thisTick();

		//the bigger the outRate, the less health is damaged
		assertTrue(valve1.getHealth()<valve2.getHealth());
		//and Health decreases more if there is a bigger pressure
		assertTrue(valve1.getPressure()>valve2.getPressure());

	}
	
	
	@Test
	public void testPassThrough(){
		turbine.getNewHydroState().receive(100, false);
		thisTick();
		
		//turbine lets out 10 of steam to valve1
		assertEquals((valve1.getNewHydroState().getSteamLevel()), 10);
		//valve1 is open, so it lets out 10 to condenser
		assertEquals((condenser.getNewHydroState().getSteamLevel()), 10);
		//the valve is destroyed, but then it acts like a pipe (or open valve) and passes evrything forward
		valve1.setHealth(0);
		
		thisTick();
		thisTick();
		assertEquals((condenser.getNewHydroState().getSteamLevel()), 30);
		//condenser.setHealth(0);
		
		System.out.println(condenser.getNewHydroState());
		thisTick();
		
		System.out.println(condenser.getNewHydroState());
		thisTick();
	//	System.out.println(valve1.getNewHydroState().getSteamLevel());
		System.out.println(condenser.getNewHydroState());
		
	}
	
	
	
	public void thisTick(){
		turbine.tick();
		turbine2.tick();
		valve1.tick();
		valve2.tick();
		pump.tick();
	}

}
